import sqlite3
import os

# 连接到 SQLite 数据库
# 若数据库文件不存在，会自动创建
# 查看当前工作目录
print(f"当前工作目录: {os.getcwd()}")

# 明确指定数据库文件路径
db_path = 'd:/MyCode/python_project/Script/example.db'
conn = sqlite3.connect(db_path)

# 创建游标对象
cursor = conn.cursor()

# 创建表
create_table_query = '''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    age INTEGER
)
'''
cursor.execute(create_table_query)

# 插入数据
insert_query = "INSERT INTO users (name, age) VALUES (?, ?)"
cursor.execute(insert_query, ('Alice', 25))
cursor.execute(insert_query, ('Bob', 30))

# 提交事务
conn.commit()

# 查询数据
select_query = "SELECT * FROM users"
cursor.execute(select_query)
rows = cursor.fetchall()
for row in rows:
    print(row)

# 关闭游标和连接
cursor.close()
conn.close()
